package com.czxy.user.service;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.czxy.domain.TbUser;
import com.czxy.user.utils.SmsUtil;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.aliyuncs.exceptions.ClientException;

import com.czxy.user.service.UserService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * （自定义类描述）
 *
 * @author chen
 * @version 1.0
 * @since 2021/7/6 18:32
 */
@Service
@Transactional
public class SmsService {

    //Redis
    @Resource
    private StringRedisTemplate stringRedisTemplate;

//    发送短信
    public boolean sendSmsCai(TbUser user) throws ClientException {
        SendSmsResponse rel = SmsUtil.sendSms(user.getPhone(), "找活儿", VerifyCode(user.getPhone()));
        if (rel.getCode().equals("OK")){
            return true;
        }else{
            return false;
        }
    }

    public String VerifyCode(String phone){
        String ss = "";
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            ss+=random.nextInt(10);
        }
        stringRedisTemplate.opsForValue().set(phone,ss,5, TimeUnit.MINUTES);
        return ss;
    }







}
